﻿#include "InpNastranReaderHelper.h"
#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMeshPartVTK.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionShellHomogeneous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialElastic.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialDensity.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialExpansion.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialDamping.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialBehavior.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterial.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionBeam.h"
#include "FITK_Interface/FITKInterfaceModel/FITKComponentManager.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKSectionAssign.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"

namespace Nastran
{
    InpNastranReaderHelper::InpNastranReaderHelper(AbaqusData::FITKDataCase * caseData)
        :_caseData(caseData)
    {
    }

    InpNastranReaderHelper::~InpNastranReaderHelper()
    {
    }

    // Insert the cell ID into the specified group in the grouping information map
    void InpNastranReaderHelper::insertCellsGroupInfo(int groupID, int cellID)
    {
        _groupingInfo[groupID].append(cellID);
    }

    bool InpNastranReaderHelper::constructGroupWithSection(AbaqusData::FITKAbaqusPart* part)
    {
        // Check if the part pointer is valid
        if (nullptr == part)
        {
            return false;
        }

        // Iterate over the grouping information
        for (QMap<int, QList<int>>::const_iterator i = _groupingInfo.constBegin();
            i != _groupingInfo.constEnd();
            ++i)
        {
            // Create a new model set for the current PID
            Interface::FITKModelSet* pidSet = new Interface::FITKModelSet();
            //pid2Modelid[i.key()] = pidSet->getDataObjectID();
            pidSet->setModelSetType(Interface::FITKModelEnum::FMSElem);
            pidSet->setParentDataID(part->getDataObjectID());
            // Add nodes to the model set
            for (int node : i.value()) pidSet->appendMember(node);

            // Check for a valid component manager
            if (nullptr == part->getComponentManager())
            {
                _reader->consoleMessage(3, "nullptr error for component manager!");
            }
            else
            {
                // Add the model set to the component manager
                part->getComponentManager()->appendDataObj(pidSet);
            }

            // Section assignment creation
            if (nullptr == _caseData->getSectionManager())
            {
                _reader->consoleMessage(3, QString("nullptr for section manager"));
                continue;
            }

            // Retrieve section by name (PID)
            auto sectionPtr = _caseData->getSectionManager()->getDataByName(QString::number(i.key()));

            // Check if section exists
            if (nullptr == sectionPtr)
            {
                _reader->consoleMessage(2, QString("The section %1 not exists").arg(i.key()));
                continue;
            }
            else
            {
                // Check for a valid section assign manager
                if (nullptr == part->getSectionAssignManager())
                {
                    _reader->consoleMessage(3, "nullptr error for assign manager!");
                    continue;
                }
                // Create and assign section
                Interface::FITKSectionAssign* assign = new Interface::FITKSectionAssign;
                assign->assign(pidSet->getDataObjectID(), sectionPtr->getDataObjectID());
                part->getSectionAssignManager()->appendDataObj(assign);
            }
        }

        // Return true if the function completes successfully
        return true;
    }

    // Set the reader object for the InpNastranReaderHelper
    void InpNastranReaderHelper::setReader(FITKNastranBDFReader * reader)
    {
        _reader = reader;
    }

}
